const app = getApp();
var util = require('../../../utils/util.js'), _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    serviceYears: 1,
    hasCar: '否',
    serviceCity: {
      cityCode: '',
      cityName: ''
    },
    serviceDistrict: '',
    serviceDistrictText: '',
    serviceType: '',
    serviceTypeText: '',
    serviceBrands: '',
    serviceBrandsText: '',
    registrationMessage: '',
    submitBlooen: true,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    _this.setData({
      userInfo: app.globalData.xieYouUserInfo,
      registrationMessage: prevPage.data.registrationMessage
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tableServiceYears: function (e) {
    var serviceYears = e.detail.value;
    this.setData({
      serviceYears: serviceYears
    })
  },
  tableHasCar: function (e) {
    var hasCar = e.detail.value ? '是' : '否';
    this.setData({
      hasCar: hasCar
    })
  },
  // 选择所在地区
  serviceCityFun: function () {
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: '/pages/become-mechanic/choose-city/choose-city',
      })
    }
  },
  // 选择区域
  serviceDistrictFun: function () {
    var serviceCity = _this.data.serviceCity;
    // 判断是否选择了地区
    if (serviceCity.cityName){
      if (!_this.data.buttonClicked) {
        util.buttonClicked(_this);
        wx.navigateTo({
          url: '/pages/become-mechanic/choose-district/choose-district?cityName=' + serviceCity.cityName + '&provinceId=' + serviceCity.cityCode,
        })
      }
    }else{
      //提示 需要先选所在地区
      util.alertMsg('提示', '需要先选择服务所在地', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          wx.navigateTo({
            url: '/pages/become-mechanic/choose-city/choose-city',
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
    }
  },
  // 选择服务类型
  serviceTypeFun: function () {
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: '/pages/become-mechanic/service-type/service-type',
      })
    }
  },
  // 选择服务机型
  serviceBrandsFun: function () {
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: '/pages/become-mechanic/expert-brands/expert-brands',
      })
    }
  },
  perfectInformation:function(e){
    var hasCar = e.detail.value.hasCar,// 是否有车
        serviceYears = e.detail.value.serviceYears, // 工龄
        serviceCity = _this.data.serviceCity,

        serviceDistrictText = _this.data.serviceDistrictText,
        serviceDistrict = _this.data.serviceDistrict,
        
        serviceTypeText = _this.data.serviceTypeText,
        serviceType = _this.data.serviceType,

        serviceBrandsText = _this.data.serviceBrandsText,
        serviceBrands = _this.data.serviceBrands;

    // 判断各项是否为空
    if (!serviceCity.cityName){
      //提示 需要先选所在地区
      util.alertMsg('提示', '需要先选择服务所在地', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          wx.navigateTo({
            url: '/pages/become-mechanic/choose-city/choose-city',
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
      return;
    }
    if (!serviceDistrictText) {
      // 判断是否有地区
      util.alertMsg('提示', '需要先选择服务覆盖区域', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          wx.navigateTo({
            url: '/pages/become-mechanic/choose-district/choose-district?cityName=' + serviceCity.cityName + '&provinceId=' + serviceCity.cityCode,
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
      return;
    }
    if (!serviceTypeText) {
      // 判断是否有维修类型
      util.alertMsg('提示', '需要先选择擅长维修类型', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          wx.navigateTo({
            url: '/pages/become-mechanic/service-type/service-type',
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
      return;
    }
    if (!serviceBrandsText) {
      // 判断是否有品牌
      util.alertMsg('提示', '需要先选择擅长维修品牌', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          wx.navigateTo({
            url: '/pages/become-mechanic/expert-brands/expert-brands',
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
      return;
    }
    var submitBlooen = _this.data.submitBlooen;
    if (submitBlooen){
      _this.submitRepairman();
    }else{
      var pages = getCurrentPages();
      util.alertMsg('提示', '您已经申请，请耐心等待', function () {
        if (!_this.data.buttonClicked) {
          util.buttonClicked(_this);
          app.refreshIndex();
          wx.navigateBack({
            delta: pages.length - 1,
          })
        }
      }, function () {
        console.log('用户点击取消');
        return;
      });
      return;
    }
  },
  submitRepairman: function (){
    var registrationMessage = _this.data.registrationMessage,
      serviceDistrict = _this.data.serviceDistrict,
      serviceType = _this.data.serviceType,
      serviceBrands = _this.data.serviceBrands;
    registrationMessage.hasCar = _this.data.hasCar=='是'?true:false;
    registrationMessage.maintainYear = _this.data.serviceYears;
    registrationMessage.serviceArea = _this.data.serviceCity.cityName;
    registrationMessage.serviceCover = [];
    registrationMessage.tecMtType = [];
    registrationMessage.tecBrands = [];
    for (var a = 0; a < serviceDistrict.length; a++){
      var DusString = {
        id: serviceDistrict[a].id,
        name: serviceDistrict[a].name,
      }
      registrationMessage.serviceCover.push(DusString);
    }
    for (var b = 0; b < serviceType.length; b++) {
      registrationMessage.tecMtType.push(serviceType[b].id);
    }
    for (var c = 0; c < serviceBrands.length; c++) {
      registrationMessage.tecBrands.push(serviceBrands[c].id);
    }
    _this.setData({
      registrationMessage: registrationMessage
    })
    //提交
    app.repairmanCompleteInfo(registrationMessage, function (res) {
      _this.setData({
        submitBlooen :false
      })
      if(res.code){
        _this.successBack();
      }else{
        var pages = getCurrentPages();
        //提示 申请失败，请稍后再试
        util.alertMsg('提示', '申请失败，请稍后再试', function () {
          if (!_this.data.buttonClicked) {
            util.buttonClicked(_this);
            app.refreshIndex();
            wx.navigateBack({
              delta: pages.length - 1,
            })
          }
        }, function () {
          console.log('用户点击取消');
          return;
        });
      }
    })
  },
  successBack:function(){
    var _this = this;
    // 提交注册申请
    var pages = getCurrentPages();
    var indexPage = pages[0];  //首页页面
    // 刷新首页
    app.refreshIndex();
    // 返回首页在进入成功页面
    // wx.navigateBack({
    //   delta: pages.length - 1
    // })
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: '/pages/become-mechanic/success/success',
      })
    }
  }
})